package com.syni.mdd.yhd.common.all.controller.protect.order.v1;

/**
 * @className BmsBusinessController
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/15 14:25
 * @VERSION 1.0
 **/

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckParams;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.yhd.common.all.service.BmsDxOrderService;
import com.syni.mdd.yhd.common.all.service.BmsDxOrderStatusService;
import com.syni.mdd.yhd.common.all.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@RequestMapping("/dxOrder")
public class BmsDxOrderController {
    AtomicInteger atomicInteger = new AtomicInteger();

    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    BmsDxOrderService bmsDxOrderService;

    @Autowired
    BmsDxOrderStatusService bmsDxOrderStatusService;

    @CheckParams(checkAuth = true,params = {"businessId","orderType"})
    @PostMapping("/commitOrder")
    public Map<String,Object> commitOrder(@RequestBody Map<String,Object> paramsMap){
        try {
            return bmsDxOrderService.orderCommit(paramsMap);
        }finally {
            if(paramsMap.containsKey("groupBuyId")){
                Integer groupId = Integer.parseInt(paramsMap.get("groupBuyId").toString());
                bmsDxOrderService.getGroupBuyMap().remove(groupId);
            }
        }
    }

    @CheckParams(checkAuth = true,params = {"orderId","orderNo"})
    @PostMapping("/orderCallBack")
    public Map<String,Object> orderCallBack(@RequestBody Map<String,Object> paramsMap){
        return bmsDxOrderService.orderCallBack(paramsMap);
    }

    @CheckParams(checkAuth = true,params = {"business_id","show_num"})
    @PostMapping("/getBusinessYearOrder")
    public Map<String,Object> getBusinessYearOrder(@RequestBody Map<String,Object> paramsMap){
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        Integer showNum = Integer.parseInt(paramsMap.get("show_num").toString());
        String endDate = null;
        if(paramsMap.containsKey("next_search") && !"".equals(paramsMap.get("next_search").toString())){
            endDate = paramsMap.get("next_search").toString();
        }
        return bmsDxOrderService.getBussinessYearOrder(businessId,showNum,endDate);
    }

    @CheckParams(checkAuth = true,params = {"business_id","show_num","search_month"})
    @PostMapping("/getBusinessMonthOrder")
    public Map<String,Object> getBusinessMonthOrder(@RequestBody Map<String,Object> paramsMap){
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        Integer showNum = Integer.parseInt(paramsMap.get("show_num").toString());
        String searchMonth = paramsMap.get("search_month").toString();
        String endDate = null;
        if(paramsMap.containsKey("next_search") && !"".equals(paramsMap.get("next_search").toString())){
            endDate = paramsMap.get("next_search").toString();
        }
        return bmsDxOrderService.getBussinessMonthOrder(businessId,showNum,searchMonth,endDate);
    }

    @CheckParams(checkAuth = true,params = {"business_id","search_day",
            "page_num","page_size"})
    @PostMapping("/getBusinessDayOrder")
    public Map<String,Object> getBusinessDayOrder(@RequestBody Map<String,Object> paramsMap){
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        String searchDay = paramsMap.get("search_day").toString();
        Integer pageNum = Integer.parseInt(paramsMap.get("page_num").toString());
        Integer pageSize = Integer.parseInt(paramsMap.get("page_size").toString());
        return bmsDxOrderService.getBussinessDayOrder(businessId,searchDay,pageNum,pageSize);
    }

    @CheckParams(checkAuth = true,params = {"order_id"})
    @PostMapping("/getBusinessOrderDetail")
    public Map<String,Object> getBusinessOrderDetail(@RequestBody Map<String,Object> paramsMap){
        Integer orderId = Integer.parseInt(paramsMap.get("order_id").toString());
        return bmsDxOrderService.showOrderDetail(orderId);
    }

    /**
     * @Description 用户端获取我的订单接口
     * @Author: cfx
     * @Date: 2019/8/2
     */

    @PostMapping("/getMyOrder")
    @CheckParams(checkAuth = true,params = {"pageNum","pageSize"})
    public Map<String,Object> getMyOrder(@RequestBody Map<String,Object> paramsMap){
        String userIdentify = paramsMap.get("userIdentify").toString();
        Integer orderStatus = 1;
        if(paramsMap.containsKey("orderStatus") && StringUtils.checkNullAndEmpty(paramsMap.get("orderStatus"))){
            orderStatus = Integer.parseInt(paramsMap.get("orderStatus").toString());
        }
        Map<String,Object> searchBody = null;
        if(paramsMap.containsKey("searchBody") && StringUtils.checkNullAndEmpty(paramsMap.get("searchBody"))){
            searchBody = (Map<String, Object>) paramsMap.get("searchBody");
        }
        if(paramsMap.containsKey("searchBodys") && StringUtils.checkNullAndEmpty(paramsMap.get("searchBodys"))){
            searchBody = JSON.parseObject(paramsMap.get("searchBodys").toString(),Map.class);
        }
        Integer pageNum = Integer.parseInt(paramsMap.get("pageNum").toString());
        Integer pageSize = Integer.parseInt(paramsMap.get("pageSize").toString());
        return bmsDxOrderService.getMyOrder(userIdentify,pageNum,pageSize,orderStatus,searchBody);
        //return null;
    }

    /**
     * @Description 用户端取消订单接口
     * @Author: cfx
     * @Date: 2019/8/2
     */
    @PostMapping("cancelOrder")
    @CheckParams(checkAuth = true,params = {"orderId"})
    public Map<String,Object> cancelOrder(@RequestBody Map<String,Object> paramsMap){
        return bmsDxOrderService.userCancelOrder(paramsMap);
    }

    /**
     * @Description 用户端获取我的订单接口
     * @Author: cfx
     * @Date: 2019/8/2
     */
    @PostMapping("deleteOrder")
    @CheckParams(checkAuth = true,params = {"orderId"})
    public Map<String,Object> deleteOrder(@RequestBody Map<String,Object> paramsMap){
        return bmsDxOrderService.userDeleteOrder(paramsMap);
    }

    /**
     * @Description 用户端通过订单id获取订单数据(消息通知使用)
     * @Author: cfx
     * @Date: 2019/8/2
     */
    @PostMapping("getNewsOrderDisplay")
    @CheckParams(checkAuth = true,params = {"orderId"})
    public Map<String,Object> userGetNewsOrderDisplay(@RequestBody ParamsMap paramsMap){
        return bmsDxOrderService.userGetNewsOrderDisplay(paramsMap);
    }


    /**
     * @Description 用户端单条订单退款详情
     * @Author: cfx
     * @Date: 2019/8/2
     */
    @PostMapping("getRefundOrderDetail")
    @CheckParams(checkAuth = true,params = {"orderId","groupBuyUseId"})
    public Map<String,Object> getRefundOrderDetail(@RequestBody ParamsMap paramsMap){
        return bmsDxOrderService.getRefundOrderDetail(paramsMap);
    }



    /**
     * @Description 用户端查看是否有存在相同的团购订单
     * @Author: cfx
     * @Date: 2019/8/2
     */
    @PostMapping("findIsBuySameGroupBuy")
    @CheckParams(checkAuth = true,params = {"groupBuyId"})
    public Map<String,Object> findIsBuySameGroupBuy(@RequestBody Map<String,Object> paramsMap){
        return bmsDxOrderService.findIsBuySameGroupBuy(paramsMap);
    }

    /**
     * @Description 订单详情
     * @Author: cfx
     * @Date: 2019/8/2
     */
    @PostMapping("orderDetail")
    @CheckParams(params = {"orderId"})
    public Map<String,Object> orderDetail(@RequestBody ParamsMap paramsMap){
        return bmsDxOrderService.orderDetail(paramsMap);
    }




}
